package com.company;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

import static com.company.RB.getColor;

/**
 * 分析工具
 */
public class Analyse {

    public static void scan(){
        File file = new File("log");
        OutputStreamWriter writer = null;
        try {
            //屏幕大小
            Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
            int width = (int) dimension.getWidth();
            int height = (int) dimension.getHeight();

            //截屏
            BufferedImage bufferedImage = RB.createScreenCapture(new Rectangle(width, height));

            //屏幕点分析文件
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)));
            //分析
            int pre = 0;
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int rgb = bufferedImage.getRGB(x, y);
                    if (rgb==pre) {
                        continue;
                    }
                    pre = rgb;
                    Color color = new Color(rgb);
                    System.out.println("x:" + x + " y:" + y + " color:" + color);
                    writer.write("x:" + x + " y:" + y + " color:" + color + "\n");
                    if (x%10==0){
                        writer.flush();
                    }
                }
            }

            //截屏图片文件截屏图片
            try {
                ImageIO.write(bufferedImage, "bmp",
                        new File("capture" + System.currentTimeMillis() + ".bmp"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("ok");

            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void monitor(){
        //获取窗口锚点
        Point anchor = new Finder().findAnchor();

        //计算监控点
        int mx = anchor.x + 1014;
        int my = anchor.y + 590;
        OutputStreamWriter writer = null;
        int i = 0;

            try {
                writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("logbt1")));
                while (i<1000) {
                    Thread.sleep(300L);
                    Color color = RB.getColor(mx, my);
                    System.out.println(i + " : " + color);
                    writer.write(i + " : " + color + "\n");
                    if (i % 100 == 0) {
                        writer.flush();
                    }
                    i++;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        if (writer!=null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        monitor();
//        scan();

    }
}
